<template>
  <div style="padding:10px">
    <div style="margin: 10px  0">
      <!--      功能区域-->
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
      <!--      <el-button type="primary">删除</el-button>-->
    </div>

    <div style="margin: 10px 0">
      <!--      搜索区域-->
      <el-input v-model="search" placeholder="输入关键字"  style="width: 20%" clearable @input="change($event)" />
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>
    <!--    <el-button>按钮</el-button>-->
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="id" sortable label="ID" width="80" />
      <el-table-column prop="teaId" label="工号" width="200px" />
      <el-table-column prop="teaName" label="姓名"  width="100px"/>
      <el-table-column prop="teaSex" label="性别" width="100px" />
      <el-table-column prop="teaPhone" label="手机号" width="200px"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button  @click="handleEdit(scope.row)" size="small" type="primary">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" size="small" >删除</el-button>
            </template>
          </el-popconfirm>
          <!--          <el-button size="small" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <el-dialog v-model="dialogVisible" title="Tips" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="工号" >
            <el-input v-model="form.teaId" style="width: 80%" disabled="false" />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.teaName" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.teaPhone" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.teaSex" >
              <el-radio label="男" size="large">男</el-radio>
              <el-radio label="女" size="large">女</el-radio>
              <el-radio label="未知" size="large">未知</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save" >确认</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>


import request from "../utils/request";

export default {
  name: 'Teacher',

  components: {

  },
  data(){
    return{
      form:{},
      tableData,
      currentPage:1,
      pageSize:10,
      total:10,
      dialogVisible:false
    }
  },
  created() {//加载方法
    this.load()
  },
  methods:{
    load(url, config){
      request.get("/api/teacher",{
        params:{
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    save(){
      if(this.form.id){
        //更新
        request.put("/api/teacher", this.form).then(res => {
          console.log(res)
          if (res.code == '200'){
            this.$message({
              type:"success",
              message:"更新成功"
            })
          }else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()//刷新表格数据
          this.dialogVisible = false//关闭弹窗
        })
      }else{
        //新增
        request.post("/api/teacher", this.form).then(res => {
          console.log(res)
          if (res.code == '200'){
            this.$message({
              type:"success",
              message:"新增成功"
            })
          }else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()//刷新表格数据
          this.dialogVisible = false//关闭弹窗
        })
      }

    },
    add(){
      this.dialogVisible = true;
      this.form = {};
      this.form.teaId = this.getProjectNum() + Math.floor(Math.random())
    },
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true;
    },
    handleSizeChange(){
      this.load()
    },
    handleCurrentChange(){
      this.load()
    },
    handleDelete(id){
      console.log(id)
      request.delete("/api/teacher/"+ id).then(res => {
        if (res.code == '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()//重新加载删除之后的
      })
    },
    change(e){
      this.$forceUpdate()
    },
    getProjectNum (){
      const projectTime = new Date() // 当前中国标准时间
      const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
      const Month = projectTime.getMonth() + 1 // 获取中国区月份
      const Day = projectTime.getDate() // 获取几号
      var CurrentDate = String(Year)
      if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
        CurrentDate += Month
      } else {
        CurrentDate += '0' + Month
      }
      if (Day >= 10) {
        CurrentDate += Day
      } else {
        CurrentDate += '0' + Day
      }
      return CurrentDate

    }
  }
}
const tableData = [

]
</script>
